package com.lx.encryption.plugin;

import com.lx.encryption.plugin.util.JarUtils;
import com.lx.encryption.plugin.util.Log;
import org.apache.maven.model.Build;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import java.io.File;

/**
 * 加密插件入口
 *
 * @author liuzhiqiang
 */
@Mojo(name = "encryption", defaultPhase = LifecyclePhase.PACKAGE)
public class EncryptionMojo extends AbstractMojo {

    @Parameter(defaultValue = "${project}", readonly = true, required = true)
    private MavenProject project;

    @Parameter
    private String[] packages;

    @Parameter
    private String password;

    @Parameter(defaultValue = "class", readonly = true, required = true)
    private String[] suffix;

    @Parameter
    private String[] specifyFile;

    /**
     * 执行加密过程
     */
    @Override
    public void execute() {
        Log.info("密码：[" + password + "]");
        Log.info("packages：[" + packages + "]");
        Log.info("suffix：[" + suffix + "]");
        Log.info("specifyFile：[" + specifyFile + "]");

        Long startDate = System.currentTimeMillis();
        Log.info("加密处理开始[" + startDate + "]");
        Build build = project.getBuild();
        String targetJar = build.getDirectory() + File.separator + build.getFinalName()
                + "." + project.getPackaging();
        Log.info("获得目标jar[" + targetJar + "]");

        try {
            JarUtils jarUtils = new JarUtils(password, packages, suffix, specifyFile);
            jarUtils.writeJarFile(targetJar);
            Long endDate = System.currentTimeMillis();
            Log.info("加密处理结束[" + endDate + "]");
            Log.info("用时[" + (endDate - startDate) / 1000 + "]秒");
        } catch (Exception e) {
            Log.info("加密处理失败");
            e.printStackTrace();
        }

    }
}
